package cn.e3mall.search.dao;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.e3mall.common.pojo.SearchItem;
import cn.e3mall.common.pojo.SearchResult;

@Repository
public class SearchDao {
	
	@Autowired
	private SolrServer solrServer;

	public SearchResult search(SolrQuery solrQuery) throws SolrServerException {
		//创建SearchResult
		SearchResult result = new SearchResult();
		
		//执行查询，返回响应
		QueryResponse response = solrServer.query(solrQuery);
		
		//得到文档集
		SolrDocumentList solrDocumentList = response.getResults();
		
		//获取总记录数
		long recordCount = solrDocumentList.getNumFound();
		
		//设置总记录数
		result.setRecordCount(recordCount);
		
		//拿到高亮文档集
		Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
		
		List<SearchItem> searchItemList = new ArrayList<SearchItem>();
		
		//遍历文档集，转换成List<SearchItem>
		for(SolrDocument solrDocument:solrDocumentList) {
			//创建searchItem
			SearchItem searchItem = new SearchItem();
			
			//设置数据
			searchItem.setId((String) solrDocument.get("id"));
			searchItem.setSell_point((String) solrDocument.get("item_sell_point"));
			searchItem.setPrice((long) solrDocument.get("item_price"));
			searchItem.setImage((String) solrDocument.get("item_image"));
			searchItem.setCategory_name((String) solrDocument.get("item_category_name"));
			
			List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
			
			String title = "";
			if( list!= null && list.size()>0) {
				//设置高亮数据
				title = list.get(0);
			}else {
				//设置未高亮数据
				title = (String) solrDocument.get("item_title");
			}
			
			searchItem.setTitle(title);
			//添加searchItem
			searchItemList.add(searchItem);
		}
		
		//设置List<SearchItem>
		result.setItemList(searchItemList);
		
		//返回SearchResult
		return result;
	}
	
	//根据商品对象同步索引库中
	public void addItemToIndexHubByItemId(SearchItem searchItem) throws Exception {
		
		//创建文档
		SolrInputDocument document = new SolrInputDocument();
		
		//添加域，并进行数据的交换
		document.addField("id", searchItem.getId());
		document.addField("item_title", searchItem.getTitle());
		document.addField("item_sell_point", searchItem.getSell_point());
		document.addField("item_price", searchItem.getPrice());
		document.addField("item_image", searchItem.getImage());
		document.addField("item_category_name", searchItem.getCategory_name());
		
		//添加文档到solrServer
		solrServer.add(document);
		
		//提交
		solrServer.commit();
	}

	public void deleteItemToIndexHubByItemId(String[] ids) throws Exception {
		
		//工具solrj删除索引库对应id的商品
		solrServer.deleteById(Arrays.asList(ids));
		
		//提交
		solrServer.commit();
	}
	
}
